<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <!-- logo -->
                <img @click="toWelcome" id="logo" src="../assets/logo.jpg" alt="" />
                <!-- 顶部标题 -->
                <span>电商后台管理系统</span>
            </div>
            <el-button type="danger" @click="logOut"> 退出 </el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu router background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
                    :collapse="isCollapse" :collapse-transition="false" :default-active="activePath"
                    class="el-menu-vertical-demo">
                    <!-- 一级菜单的循环渲染 -->
                    <el-submenu :index="String(item.id)" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="iconClass[item.id]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单循环渲染 -->
                        <el-menu-item @click="saveActivePath('/' + subItem.path)" v-for="subItem in item.children" :index="'/' + subItem.path">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    name: 'home',
    data() {
        return {
            menuList: [],
            activePath: '', // 默认激活的菜单
            iconClass: {
                // 键名就是菜单对应的id
                125: "iconfont icon-user",
                103: "iconfont icon-tijikongjian",
                101: "iconfont icon-shangpin",
                102: "iconfont icon-danju",
                145: "iconfont icon-baobiao",
            },
            isCollapse: false,
        }
    },
    created() {
        // 调用菜单请求方法
        this.getMenus()
    },
    methods: {
        // 退出
        logOut() {
            this.$confirm('确认退出系统吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                sessionStorage.removeItem('token')

                this.$message({
                    type: 'success',
                    message: '退出成功!'
                });

                this.$router.push('/login')
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },

        // 请求菜单数据
        async getMenus() {
            const res = await this.$http.get('menus')
            // console.log(res);
            if (res.status == 200) {
                this.menuList = res.data.data

                // 取出存起来的path路径默认高亮
                const path = sessionStorage.getItem('activePath')
                if(path) {
                    this.activePath = path
                }
            } else {
                this.$message.error(res.data.meta.msg)
            }
        },

        // 点击菜单栏的折叠按钮
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        },

        // 点击图标跳回欢迎页面
        toWelcome() {
            this.activePath = ''
            sessionStorage.removeItem('activePath')

            this.$router.push({ name: 'welcome' })
        },

        // 保存当前选中的菜单项
        saveActivePath(path) {
            sessionStorage.setItem('activePath', path)
        }
    }
}
</script>
<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 20px;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
        display: flex;
        align-items: center;

        span {
            margin-left: 15px;
        }
    }
}

.el-aside {
    background-color: #333744;
    transition: all 0.3s;

    .el-menu {
        border-right: none;
    }
}

.el-main {
    background-color: #eaedf1;
}

.iconfont {
    margin-right: 10px;
}

.toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}

#logo {
    width: 30px;
    cursor: pointer;
}
</style>